﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Presentation.Models
{
    public class Film
    {
        private static SqlConnection connection;

        public Film()
        {
        }

        public Film(int id)
        {

        }

        public int Id { get; set; }

        public string Titre { get; set; }

        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime DatePublication { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:C0}")]
        [Display(Name = "Date de publication ")]
        public int Prix { get; set; }

        public string Genre { get; set; }



        public static List<Film> getListFilms()
        {
            List<Film> listeFilms = new List<Film>();
            

            try
            {
                OpenConnection();

                SqlCommand reqSQL = new SqlCommand("SELECT * FROM Film");


                reqSQL.Connection = connection;

                SqlDataReader dr = reqSQL.ExecuteReader();



                while (dr.Read())
                {
                    Film nouveauFilm = new Film();
                    nouveauFilm.Id = dr.GetInt32(0);
                    nouveauFilm.Titre = dr.GetString(1);
                    nouveauFilm.DatePublication = dr.GetDateTime(2);
                    nouveauFilm.Prix = dr.GetInt32(3);
                    nouveauFilm.Genre = dr.GetString(4);
                    listeFilms.Add(nouveauFilm);
                }

            }
            catch (Exception)
            {

            }
            finally
            {
                //connection.Close();
            }

            return listeFilms;
        }

        private Film getFilm(int id)
        {
            Film monFilm = null;
            try
            {
                OpenConnection();

                SqlCommand reqSQL = new SqlCommand("SELECT * FROM FILM WHERE idFilm = @id");
                reqSQL.Parameters.Add(new SqlParameter("@id", id));

                reqSQL.Connection = connection;

                SqlDataReader dr = reqSQL.ExecuteReader();



                if (dr.Read())
                {
                    monFilm = new Film();
                    monFilm.Id = id;
                    monFilm.Titre = dr.GetString(1);
                    monFilm.DatePublication = dr.GetDateTime(2);
                    monFilm.Prix = dr.GetInt32(3);
                    monFilm.Genre = dr.GetString(4);
                }

            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }
            return monFilm;

        }

        private void updateFilm(Film updatedMovie)
        {
            try
            {
                OpenConnection();

                SqlCommand reqSQL = new SqlCommand("UPDATE Film SET titre = @titre, datePublication = @date, prix = @prix, genre = @genre WHERE idFilm = @id");

                reqSQL.Parameters.Add(new SqlParameter("@id", updatedMovie.Id));
                reqSQL.Parameters.Add(new SqlParameter("@titre", updatedMovie.Titre));
                reqSQL.Parameters.Add(new SqlParameter("@date", updatedMovie.DatePublication));
                reqSQL.Parameters.Add(new SqlParameter("@prix", updatedMovie.Prix));
                reqSQL.Parameters.Add(new SqlParameter("@genre", updatedMovie.Genre));

                reqSQL.Connection = connection;

                reqSQL.ExecuteNonQuery();
            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }

        }

        private static void OpenConnection()
        {
            connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["MovieDBPresentation"].ConnectionString;
            connection.Open();
        }
    }
}